(function () {

    "use strict";

    var api = {
        getOrderDetailUrl: '/api/order/content',
        chgOrder: '/api/order/chgStatus'
    };
    sm.controller('wx-order-detail-controller', ['$scope', '$http', 'Toast', function ($scope, $http, Toast){

        $scope.order = {};

        var search = window.location.search;
        var orderId = search.split('=')[1];

        $http.get(api.getOrderDetailUrl, {
            params: {
                orderId: orderId
            }
        }).success(function (res) {
            if (res.state) {
                $scope.order = res.data;
            }
        });

        $scope.cancel = function () {
            if ($scope.order.status != 'Unpaid') {
                return false;
            }

            Toast.loading();
            $http.post(api.chgOrder, {
                'orderId': $scope.order.orderId,
                'status': 'Cancel'
            }).success(function (res) {
                Toast.stop();
                if (res.state) {
                    $scope.order.status = 'Cancel';
                    Toast.success({title: '取消成功'});
                } else {
                    Toast.error({title: '网络错误'});
                }
            }).error(function () {
                Toast.end();
            });
        };

        /**
         * 恢复
         * @returns {boolean}
         */
        $scope.recover = function () {

            if ($scope.order.status != 'Cancel') {
                return false;
            }

            Toast.loading();
            $http.post(api.chgOrder, {
                'orderId': $scope.order.orderId,
                'status': 'Unpaid'
            }).success(function (res) {
                Toast.stop();
                if (res.state) {
                    $scope.order.status = 'Unpaid';
                    Toast.success({title: '恢复成功'});
                } else {
                    Toast.error({title: '网络错误'});
                }
            }).error(function () {
                Toast.end();
            });

        };


        $scope.viewExp = function () {

            if (!$scope.order.distribution.expressNo) {
                return false;
            }

            location.href = '/order/express?orderId=' + $scope.order.orderId;
        };


    }]);
})();

